package com.zhang.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.gulimall.common.constant.WareConstant;
import com.zhang.gulimall.common.utils.PageUtils;
import com.zhang.gulimall.common.utils.Query;
import com.zhang.gulimall.ware.vo.DoneVo;
import com.zhang.gulimall.ware.vo.ItemVo;
import com.zhang.gulimall.ware.vo.MergeVo;
import com.zhang.gulimall.ware.dao.PurchaseDao;
import com.zhang.gulimall.ware.dao.PurchaseDetailDao;
import com.zhang.gulimall.ware.entity.PurchaseDetailEntity;
import com.zhang.gulimall.ware.entity.PurchaseEntity;
import com.zhang.gulimall.ware.entity.WareSkuEntity;
import com.zhang.gulimall.ware.feign.SkuInfoFeignService;
import com.zhang.gulimall.ware.service.PurchaseDetailService;
import com.zhang.gulimall.ware.service.PurchaseService;
import com.zhang.gulimall.ware.service.WareSkuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Resource
    PurchaseDetailService detailService;

    @Resource
    PurchaseDetailDao  detailDao;

    @Resource
    WareSkuService wareSkuService;

    @Resource
    SkuInfoFeignService skuInfoFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByParams(Map<String, Object> params) {
//        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
//        String wareId = (String) params.get("wareId");
//        String skuId = (String) params.get("skuId");
//        if (!wareId.isEmpty()){
//            wrapper.lambda().eq(PurchaseEntity::getWareId,wareId);
//        }
//        if (!wareId.isEmpty()){
//            wrapper.lambda().eq(PurchaseEntity::getSkuId,skuId);
//        }
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnReceive(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        List<Integer> list = new ArrayList<Integer>(){};
        list.add(0);
        list.add(1);
        wrapper.lambda().in(PurchaseEntity::getStatus, list);
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void merge(MergeVo mergeVo) {
        String purchaseId = mergeVo.getPurchaseId();
        if (mergeVo.getPurchaseId() == null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(1);
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        String finalPurchaseId = purchaseId;
        mergeVo.getItems().forEach(item->{
                PurchaseDetailEntity entity = detailService.getById(item);
                entity.setPurchaseId(finalPurchaseId);
                entity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
                detailService.updateById(entity);
            });
    }

    @Override
    public void received(List<String> ids) {
        ids.forEach(id->{
            PurchaseEntity purchaseEntity = this.baseMapper.selectById(id);
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            this.baseMapper.updateById(purchaseEntity);
            QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(PurchaseDetailEntity::getPurchaseId,id);
            List<PurchaseDetailEntity> entities = detailDao.selectList(wrapper);
            entities.forEach(entity->{
                entity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                detailDao.updateById(entity);
            });
        });
    }

    @Transactional
    @Override
    public void done(DoneVo doneVo) {
        boolean flag = false;
        //筛选异常单
        for (ItemVo item : doneVo.getItems()) {
            if(item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()){
                flag = true;
                break;
            }
        }
        //设置采购单情况
        PurchaseEntity purchaseEntity = this.baseMapper.selectById(doneVo.getId());
        if (flag){
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        } else {
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.FINISH.getCode());
        }
        this.baseMapper.updateById(purchaseEntity);
        //设置详细采购情况
        doneVo.getItems().forEach(itemVo -> {
            PurchaseDetailEntity entity = detailDao.selectById(itemVo.getItemId());
            entity.setStatus(itemVo.getStatus());
            detailDao.updateById(entity);
            //添加库存
            QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(WareSkuEntity::getSkuId,entity.getSkuId()).eq(WareSkuEntity::getWareId,entity.getWareId());
            WareSkuEntity one = wareSkuService.getOne(wrapper);
            if (one != null){
                if (entity.getStatus() != WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()){
                    one.setStock(one.getStock() + entity.getSkuNum());
                }
                wareSkuService.updateById(one);
            } else {
                WareSkuEntity wareSkuEntity = new WareSkuEntity();
                wareSkuEntity.setStock(wareSkuEntity.getStock() + entity.getSkuNum());
                wareSkuEntity.setSkuId(entity.getSkuId());
                wareSkuEntity.setWareId(entity.getWareId());
                String name = skuInfoFeignService.selectSkuName(entity.getSkuId());
                wareSkuEntity.setSkuName(name);
                wareSkuService.save(wareSkuEntity);
            }
        });
    }

}